<?
include 'login.php';
include 'menu.php';
include '../server/include.php';
ini_set('display_errors', 1);

new lib_log;

class lib_log
{
    var $db;
    function  __construct() {
        $this->showlog();
    }


    function showlog()
    {
        $id = mysql_escape_string($_GET['id']);
        if(isset($_POST['clear']))
        {
            _database::query('DELETE FROM `log` WHERE `user_id`='.$id);
        }
        if(!$id) $id = 0;
        $LIMIT = isset($_GET['page'])?(int)$_GET['page']*20:0;

        $query = 'SELECT * FROM  `log` WHERE `user_id`='.$id.' ORDER BY `logtime` DESC LIMIT '.$LIMIT.',20';

        $logs = _database::toarray($query);
        print <<<SCRIPT
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="/admin/css/log.css" type="text/css" />
    <script type="text/javascript" src="/admin/js/tabber.js"></script>
    <script type="text/javascript" src="/admin/js/prototype.js"></script>
    <title>Request/Response Log</title>

    <script type="text/javascript">
       document.write('<style type="text/css">.tabber{display:none;}<\/style>');
       /*var tabberOptions = {

           'onClick': function(argsObj) {

               var t = argsObj.tabber;
               var id = t.id;
               var i = argsObj.index;
               var div = this.tabs[i].div;
               var page = i+1;

               //if (id == 'pagination') {


                   div.innerHTML = "<p>Loading...<\/p>";


                   var url = 'log.php';
                   var pars = 'p='+i;
                   var myAjax = new Ajax.Updater(div, url, {method:'get',parameters:pars});
               //}
           },

           'onLoad': function(argsObj) {
               argsObj.index = 0;
               var t = argsObj.tabber;
               var id = t.id;
               this.onClick(argsObj);
           }

       }*/

       /*
       var tabberOptions = {

          'onClick': function(argsObj) {

            var t = argsObj.tabber; // Tabber object
            var i = argsObj.index; // Which tab was clicked (0..n)
            var div = this.tabs[i].div; // The tab content div

            // Display a loading message
            div.innerHTML = "<p>Loading...<\/p>";

            // Fetch some html depending on which tab was clicked
            var url = 'http://89.108.88.14/restaurant/log.php';
            var pars = 'p='+i;
            var myAjax = new Ajax.Updater(div, url, {method:'get',parameters:pars});
          },

          'onLoad': function(argsObj) {
            // Load the first tab
            argsObj.index = 0;
            this.onClick(argsObj);
          }

        }*/
    </script>


</head>


<body>
SCRIPT;
        echo '<div style="width:49%;float:left"><a href="?id='.$id.'&page='.max(0,(int)$_GET['page']-1).'"><<</a></div>';
        echo '<div style="width:49%;text-align:right;float:left"><a href="?id='.$id.'&page='.max(0,(int)$_GET['page']+1).'">>></a></div><br clear="all"/>';
        echo '<form><input name=id value=\''.$id.'\'/><input type=submit value=\'>>\'></form>';
        echo '
<script>
function wipeall()
{
    if(confirm("точно всех удалить?"))
{
if(confirm("ВСЕХ!? ВООБЩЕ ВСЕХ КТО ЕСТЬ НА СЕРВЕРЕ?"))
{
if(confirm("ЕЩЕ МОЖНО ОСТАНОВИТЬСЯ!!! ТОЧНО ВСЕХ ЗАНУЛИТЬ? ВАШ IP СОХРАНЕН"))
{
    document.getElementById(\'trt\').submit()
}
}
}
}
</script>
<form method=post  action="?id='.$id.'"><input type=hidden name=id value=\''.$id.'\'/>
<input type=submit name=clear value=\'clearlog\'>
<input type=submit name=WIPE value=\'wipe\'>
</form>
<form method=post id=trt action="?id='.$id.'">
<input name=WIPEAL1dL type=hidden value=ohno >
<input type=button onclick="javascript:wipeall()"  value=\'wipe all users\'>
</form>';

        //print '<table width=100% cellspacing="5" cellpadding="1" border="0">';

        if($logs)
        foreach($logs as $logrow)
        {

            $time = date('Y-m-d H:i:s',$logrow['logtime']);
            $request_decoded = @json_decode($logrow['request']);
            $type = $request_decoded->method;
            $execution_time = $logrow['execution_time'];
            $request = '<pre>'.$logrow['request']."\n\n".print_r($request_decoded,1).'</pre>';
            $response_decoded = @json_decode($logrow['response']);
            $response = '<pre>'.print_r($response_decoded,1).'</pre>';

            $mysqls = '';
            $logsa = '';
            $log_decoded =  @json_decode($logrow['log']);
         
            foreach($log_decoded as $row)
            {
                if($row[3]) // mysql
                {
                    $mysqls .= '<div style="padding:2px">'.$row[1].' <b>'.$row[2].' </b>s.</div>';
                }
                else
                {
                    $logsa .= '<div style="padding:2px">'.$row[2].' <b>'.$row[1].' </b></div>';
                }
            }
            

            $tmp = '';

            $tmp .= <<<OHUET
    <b>$time</b>
    <div class="tabber">

         <div class="tabbertab">
          <h2>Request</h2>
          <p>
            <table border='0'>
            <tr><td>Type: </td><td><b>$type</b></td></tr>
            <tr><td><!--PlayerId: --></td><td><!-- $id --></td></tr>
            <tr><td></td><td>$execution_time</td></tr>
            <tr><td></td><td>$request</td></tr>
            <!--tr><td>MySQL Time: </td><td>{mysqlTime}</td></tr-->
            </table>
          </p>
         </div>
         <div class="tabbertab">
          <h2>Response</h2>
          <p><pre>$response</pre></p>
         </div>
         <div class="tabbertab">
          <h2>MySQL</h2>
          <p>$mysqls</p>
         </div>
         <div class="tabbertab">
          <h2>Debug</h2>
          <p>$logsa</p>
         </div>
         <div class="tabbertab">
          <h2>Memcached</h2>
          <p>$mc</p>
         </div>
         <div class="tabbertab">
          <h2>?</h2>
          <p>
          <ul>
          </ul>
          </p>
         </div>

    </div>
OHUET;

            //$tmp .= '<tr><td style="background:#ebebeb"><h2 class="trigger" style="cursor:pointer;"><b>request</b> [#'.($request->t).']</h2><div class="toggle_container"><pre>'.print_r($request,1).'</pre></div></td></tr>';
            //$tmp .= '<tr><td style="background:#ebebeb"><h2 class="trigger" style="cursor:pointer;"><b>response</b></h2><div class="toggle_container"><pre>'.$response.'</pre></div></td></tr>';
            //$tmp .= '<tr><td style="background:#ebebeb"><h2 class="trigger" style="cursor:pointer;"><b>mysql</b></h2><div class="toggle_container">'.$mysqls.'</div></td></tr>';
            //$tmp .= '<tr><td style="background:#ebebeb"><h2 class="trigger" style="cursor:pointer;"><b>log</b></h2><div class="toggle_container">'.$logs.'</div></td></tr>';
            //$tmp .= '<tr><td style="background:#ebebeb"><h2 class="trigger" style="cursor:pointer;"><b>memcached</b></h2><div class="toggle_container"><pre>'.$mc.'</pre></div></td></tr>';

            //$tmp =  '<tr><td style="background:#ababab"><b>'.$time.'</b></td></tr>'.$tmp;
            print $tmp;
        }
        print '</table>';
    }
}

?>